<?php
namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Loglogin;
use App\Models\Project;
use App\Models\RoleUser;
use App\Models\Startup;
use App\Models\User;
use App\Models\UserCategory;
use App\Models\UserInfo;
use App\Models\Zone;
use Illuminate\Http\Request;
use \Excel;

class UserActionController extends CommonController
{

    public function getIndex()
    {
        $users = User::with('projects')->orderBy('reg_time', 'desc')->orderBy('uid', 'asc')->paginate(10);
        return view('useraction.index')->with('users',$users)->with("actions", $this->actions);
    }


    /**
     * 执行一个搜索请求
     *
     */
    public function search(Request $request)
    {
        $data = $request->input();

        $sql = User::with('roleUser.role')->orderBy('reg_time', 'desc')->orderBy('uid', 'asc');
        if (isset($data['username']) && $data['username'] != '') {
            $sql->where('user.username', 'like', '%' . trim($data['username']) . '%');
        }

        if (isset($data['nickname']) && $data['nickname'] != '') {
            $sql->orWhere('user.nickname', 'like', '%' . trim($data['nickname']) . '%');
        }

        if (isset($data['realname']) && $data['realname'] != '') {
            $sql->orWhere('user.realname', 'like', '%' . trim($data['realname']) . '%');
        }


        if (isset($data['role']) && $data['role'] != '') {
            $uids = RoleUser::where('role_id', $data['role'])->lists('uid');
            $sql->whereIn('user.uid', $uids);
        }

        $users = $sql->paginate(10);

        return view('useraction.search')->with("users", $users)->with("username", trim($data['username']))
            ->with("nickname", trim($data['nickname']))->with("realname", trim($data['realname']))
            ->with("realname", $data['realname'])->with("actions", $this->actions)->with("role", $data['role']);
    }


    public function getProjects(Request $request)
    {
        $data = $request->input();
        $lists = Project::where('uid', $data['uid'])->with('user', 'city')->orderBy('created_at', 'desc')->orderBy('recommend', 'desc')->paginate(10);

        return view('useraction.projects')->with('projects', $lists)->with("actions", $this->actions);
    }



}